<?php
namespace admin\module\admin;
use inc\BaseAjax\BaseAjax;
require('../../../global.php');

class ajax extends BaseAjax
{
    
    /**
     * 列出所有的后台员工
     * @Author : bapijun
     * param search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 根据搜索参数返回符合条件的部门或者职位数组
     */
    protected function listTable()
    {
        global $db;
        if (checkVar($_GET['search'])) {
            $where['AND']['username[~]'] = $_GET['search'];
            $where['AND']['mobile[~]'] = $_GET['search'];
            $where['AND']['realname[~]'] = $_GET['search'];
        }
        if (checkVar($_GET['sort']) && checkVar($_GET['order'])) {
            $where['ORDER'][$_GET['sort']] = strtoupper($_GET['order']);
        } else {
            $where['ORDER']['tbid'] = 'DESC';
        }
        $echo['total'] = $db->count('tb_admin', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_admin', '*', $where);
        foreach ($row as $value) {
            $avatar = file_exists($value['img']) ? $value['img'] : 'admin/img/avatar.jpg';
            $tmp['preview'] = '<img src="'.SITEROOTURL.$avatar.'" width="64" height="64" />';
            $tmp['username'] = $value['username'];
            $tmp['realname'] = $value['realname'];
            $tmp['mobile'] = $value['mobile'];
            $tmp['tel'] = $value['tel'];
            if(checkAdminAuthorize('admin.edit')){
                $tmp['do'] = ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">编辑</a> ';
                if($value['tbid'] != session('admin_id')){
                    $tmp['do'] .= ' <button type="button" class="btn btn-danger btn-outline btn-xs remove" data-id="'.$value['tbid'].'">删除</button> ';
                }
            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    /**
     * 编辑或新增员工信息
     * @Author : bapijun
     * param 员工的信息
     * echo 编辑成功返回参数 y,失败返回  参数n
     */
    protected function edit()
    {
        global $db;
        $set = array(
            'mobile' => $_POST['mobile'],
            'realname' => $_POST['realname'],
            'tel' => $_POST['tel'],
            'addtime' => $_POST['addtime'],
            'authorizelist' => $_POST['authorizelist'],
            'edittime' => date('Y-m-d H:i:s'),
        );
        if($_POST['password'] != ''){
            $set['password'] = \MyEncry::sha256($_POST['password']);
        }
        if($_POST['id'] != ''){
        
            $db->update('tb_admin', $set, array(
                'tbid' => $_POST['id']
            ));
            $cb['status'] = 'y';
        }else{
        
            $set['createtime'] = date('Y-m-d H:i:s');
            $set['username'] = $_POST['username'];
            $rs = $db->insert('tb_admin', $set);
            if($rs){
                $cb['status'] = 'y';
            }else{
                $cb['status'] = 'n';
            }
        }
        echo json_encode($cb);
    }
    
    protected function delete()
    {
        global $db;
        $rs = $db->delete('tb_admin', array('tbid' => $_POST['id']));
        if($rs > 0){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }

    protected function getAuthorizeInfo()
    {
        global $db;
        global $g_admin_authorize;
        $authorizelist = explode(',', base64_decode($_GET['authorizelist']));
        foreach($g_admin_authorize as $k => $v){
            $i = 0;
            foreach($g_admin_authorize[$k] as $kk => $vv){
                if($i != 0){
                    if(in_array($k.'.'.$kk, $authorizelist)){
                        $cb[] = $v['_title'].'：'.$vv;
                    }
                }
                $i++;
            }
        }
        echo json_encode($cb);
    }
}

$ajax = new ajax();
$ajax->dealWithAjax();
